package bs.app.Client;

import java.util.Vector;

import bs.app.HTTPConnection.CConnection;
import bs.app.Main.CCanvas;
import bs.app.Res.CRess;

public class PageManager implements IDataListener
{
	public static PageManager instance;
	public static String split_char20=String.valueOf((char)20);
	public static String split_char21=String.valueOf((char)21);
	public PageManager()
	{
		
	}
	public static PageManager getInstance(){
		if(instance==null)
			instance=new PageManager();
		return instance;
	}
	public static String[] insertArray(String card[],String value)
	{
		if(card!=null)
		{
			String[]temp=card;
			card=new String[card.length+1];
			for(int i=0;i<card.length-1;i++)
			{
				card[i]=temp[i];
			}
			card[card.length-1]=value;
		}
		else
		{
			card=new String[1];
			card[0]=value;
		}
		return card;
	}
	public void Page1(String Info) 
	{
		  String itemfix[]={"item0.png","item1.png","item2.png","item3.png","item4.png","item5.png","item6.png","item7.png","item8.png","item9.png","item10.png","item11.png","item12.png","item13.png","item14.png","item15.png"};
		  String [] elementItem=null;
		  String [][] data=null;
		  elementItem=CConnection.split(Info, split_char21);
		  data=new String[elementItem.length][];
		  String[][] ImageNameServer=new String[data.length][];
		  String[]ImgClientHaveNot=null;
		  String[] ImgClientHave=null;
		  boolean kocohinh=true;
		  CCanvas.s_Instan.connect.DataListForChildrenMenu=new String[elementItem.length][2];
		  
		  Vector tempdata=new Vector();
		  
		  for(int i=0;i<data.length;i++)
		  {
			  kocohinh=true;
			  data[i]=CConnection.split(elementItem[i], split_char20);
			  ImageNameServer[i]=CConnection.split(data[i][2], "/");
			  for(int k=0;k<itemfix.length;k++)
			  {
				  // server co hinh giong voi hinh trong resouce
				  if(itemfix[k].equals(ImageNameServer[i][ImageNameServer[i].length-1].trim()))
				  {
					  kocohinh=false;
					  ImgClientHave=insertArray(ImgClientHave, itemfix[k]);
					  CCanvas.s_Instan.connect.MarkLoadImg=insertArray(CCanvas.s_Instan.connect.MarkLoadImg, "0");
					  k=itemfix.length+1;
				  }
			  }
			  if(kocohinh==true)
			  {
				  ImgClientHaveNot=insertArray(ImgClientHaveNot, ImageNameServer[i][ImageNameServer[i].length-1]);
				  CCanvas.s_Instan.connect.MarkLoadImg=insertArray(CCanvas.s_Instan.connect.MarkLoadImg, "1");
			  }
			  
			  if(data[i][data[i].length-1].trim().equals("1"))
			  {
				  tempdata.addElement(new String[]{data[i][0],data[i][1]});
			  }
		  }
		  CCanvas.s_Instan.connect.DataSearchList=new String[tempdata.size()][2];
		  for(int i=0;i<tempdata.size();i++)
		  {
			  CCanvas.s_Instan.connect.DataSearchList[i]=(String[])tempdata.elementAt(i);
		  }
		  /////////   Get Data For Children Menu
		  for(int i=0;i<data.length;i++)
		  {
			  for(int k=0;k<2;k++)
			  {
				  CCanvas.s_Instan.connect.DataListForChildrenMenu[i][k]=data[i][k];
			  }
		  }
		  
		  // get path foder
		  for(int i=0;i<1;i++)
		  {
			  for(int k=0;k<ImageNameServer[i].length-1;k++)
			  {
				  CCanvas.s_Instan.connect.PathFoder+=ImageNameServer[i][k]+"/";
			  }
		  }
		  if(ImgClientHaveNot==null)  // tat ca hinh deu co trong resouce
		  {
			    CCanvas.s_iHcanvas=CCanvas.s_Instan.getHeight();
				CCanvas.s_iWcanvas=CCanvas.s_Instan.getWidth();
				if(CCanvas.apppage1==null)
				CCanvas.apppage1=new AppPage1();
				CCanvas.apppage1.init(elementItem.length,data,ImgClientHave,null,null);
				CCanvas.CurrentScreen=CCanvas.AppPage1;
				CRess.ClearLogo();
				CCanvas.splash=null;
				CCanvas.apppage2=null;
				System.gc();
		  }
		  else   // client ko co hinh giong nhu server phai down load ve
		  {
			  CCanvas.s_Instan.connect.Page=1;
			  CCanvas.s_Instan.connect.NumberImage=elementItem.length;
			  CCanvas.s_Instan.connect.data=data;
			  CCanvas.s_Instan.connect.ImgNameServer=ImgClientHaveNot;
			  CCanvas.s_Instan.connect.ImgClientHave=ImgClientHave;
			  CCanvas.s_Instan.connect.RequestServerGetImage();
		  }
	}
	public void Page2_3_4_6(String page,String Info) 
	{
		  String [] elementItem=null;
		  String [][] data=null;
		  elementItem=CConnection.split(Info, split_char21);
		  data=new String[elementItem.length][];
		  ///////////////////////////////////////////
		  for(int i=0;i<data.length;i++)
		  {
			  data[i]=CConnection.split(elementItem[i], split_char20);
			  
		  }
		  
		  
		  String[] totalPage=CConnection.split(elementItem[elementItem.length-1], "\n");
		  CCanvas.s_Instan.connect.Page=Integer.parseInt(page.trim());
		  CCanvas.s_Instan.connect.NumberImage=elementItem.length;
		  CCanvas.s_Instan.connect.data=data;
		  if(totalPage.length==2)
		  CCanvas.s_Instan.connect.TotalRecord=Integer.parseInt(totalPage[1].trim());
		  CCanvas.s_Instan.connect.RequestServerGetImage();
	}
	public void Page5(String Info) 
	{
		  String [] elementItem=null;
		  elementItem=CConnection.split(Info, split_char20);
		  String [] brtag=null;
		  brtag=CConnection.split(elementItem[elementItem.length-1], "<br/>");
		  for(int i=0;i<brtag.length;i++)
		  {
			  if(!brtag[i].trim().equals(""))
			  {
				  int begin=brtag[i].indexOf("img ");
				  if(begin>0)
				  {
					  begin=brtag[i].indexOf("src")+5;
					  int end=brtag[i].indexOf("\"",begin);
					  brtag[i]=brtag[i].substring(begin, end);
					  CCanvas.s_Instan.connect.ImgNameServer=insertArray(CCanvas.s_Instan.connect.ImgNameServer,brtag[i].trim());
				  }
			  }
		  }
		  if( CCanvas.s_Instan.connect.ImgNameServer!=null)  // co load hinh
		  {
			  CCanvas.s_Instan.connect.Page=5;
			  CCanvas.s_Instan.connect.NumberImage=1;
			  CCanvas.s_Instan.connect.datamotchieu=elementItem;
			  CCanvas.s_Instan.connect.RequestServerGetImage();
		  }
		  else   // ko co load hinh
		  {
			  	CCanvas.s_Instan.connect.Page=5;
			  	if(CCanvas.apppage5==null)
			  	{
					 CCanvas.apppage5=new AppPage5();
				}
			 	CCanvas.apppage5.init(elementItem,null);
				CCanvas.CurrentScreen=CCanvas.AppPage5;
		  }
	}
}
